/*

Copyright (C) 2007 Thomas Geiger (tom.geiger@gmail.com)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include <octave/oct.h>

#include "globuscpp/GassServer.h"

using namespace std;
using namespace globus_server;

// have a global instance for all functions
static GassServer server;

DEFUN_DLD(gt_startgass, 
          args, 
          , 
          "Starts the GASS Server."
          ) 
{	
	static const string msg = "Usage: gt_startgass(['listenPort'])";
	
	int nargin = args.length();	
	if (nargin > 1) {
		octave_stdout << msg << endl;
		return octave_value(false);
	}
	
	// check for existing server
	if ( server.isRunning() ) {
		octave_stdout << "Server already runs at " << server.getPort() << "." << endl;
		return octave_value(false);
	}
	
	// enable security
	server.setSecure(true);
	
	if (nargin == 1) {
		int port = args(0).int_value();
		if (error_state) {
			octave_stdout << msg << endl;
			return octave_value(false);
		}
		// start with custom port
		server.setPort(port);
	}  
	// start with default port	
	return octave_value( server.start() );	
}

DEFUN_DLD(gt_stopgass, 
          args, 
          , 
          "Stops the GASS Server."
          ) 
{	
	if (!server.isRunning()) {
		octave_stdout << "Server does not run." << endl;
		return octave_value(false);
	}
	
	return octave_value( server.stop() );
}
